package com.logic.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.logic.common.ErrorMessageEnum;
import com.logic.common.domain.Code;
import com.logic.common.exception.BusinessException;
import com.logic.common.security.SecurityUtils;
import com.logic.common.util.QueryUtil;
import com.logic.common.ws.dto.system.AddressDTO;
import com.logic.system.domain.Address;
import com.logic.system.persistence.read.AddressReadMapper;
import com.logic.system.persistence.write.AddressWriteMapper;
import com.logic.system.service.IAddressService;
import com.logic.system.service.ICodeService;
import com.logic.system.ws.dto.AddressConverter;
import com.logic.system.ws.dto.AddressDto;

@Service
public class AddressService implements IAddressService {

    @Autowired
    private AddressReadMapper addressReadMapper;

    @Autowired
    private AddressWriteMapper addressWriteMapper;

    @Autowired
    private ICodeService codeService;

    public AddressDTO get(Integer id) throws BusinessException {
        Address address = addressReadMapper.selectByPrimaryKey(id);
        AddressDTO addressDTO = AddressConverter.fromAddressToDTO(address);
        getFullAddress(addressDTO);

        return addressDTO;

    }

    public int insert(Object obj) throws BusinessException {
        Address address = (Address) obj;
        Boolean isValid = this.validateForInsert(address);
        if (isValid) {
            address.updateCommonInfo(SecurityUtils.getCurrentLogin().getUserId());
            addressWriteMapper.insertSelective(address);

        }

        return address.getId();

    }

    public void update(Object obj) throws BusinessException {

        Address address = (Address) obj;
        Boolean isValid = this.validateForUpdate(address);
        if (isValid) {
            address.updateCommonInfo(SecurityUtils.getCurrentLogin().getUserId());
            addressWriteMapper.updateByPrimaryKeySelective(address);
        }

    }

    public void delete(String ids) throws BusinessException {
        String[] idArr = ids.split(",");
        for (String id : idArr) {
            AddressDTO addressDTO = get(Integer.valueOf(id));
            Address address = AddressConverter.fromDTOtoAddress(addressDTO);
            if (address == null) {
                throw new BusinessException(ErrorMessageEnum.Address_Empty_Object, Integer.valueOf(id));
            } else {
                address.updateCommonInfo(SecurityUtils.getCurrentLogin().getUserId());
                addressWriteMapper.deleteByPrimaryKey(address);
            }
        }
    }

    @Override
    public List<Object> query(QueryUtil queryUtil) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Boolean validate(Object obj) throws BusinessException {
        Address address = (Address) obj;

        if (address == null) {
            throw new BusinessException(ErrorMessageEnum.Address_NOT_Empty_Object);
        }

        if (address.getCity() == null) {
            throw new BusinessException(ErrorMessageEnum.Address_City_NOT_Empty);
        }

//      if (address.getCounty() == null) {
//          throw new BusinessException(ErrorMessageEnum.Address_County_NOT_Empty);
//      }

        if (address.getAddressLine1() == null) {
            throw new BusinessException(ErrorMessageEnum.Address_addressLine1_NOT_Empty);
        }

        if (address.getProvince() == null) {
            throw new BusinessException(ErrorMessageEnum.Address_province_NOT_Empty);
        }

        return true;
    }

    @Override
    public Boolean validateForInsert(Object obj) throws BusinessException {
        Address address = (Address) obj;

        if (address.getId() != null) {
            throw new BusinessException(ErrorMessageEnum.Address_ID_Empty_Object);
        }
        return validate(obj);
    }

    @Override
    public Boolean validateForUpdate(Object obj) throws BusinessException {

        Address address = (Address) obj;

        if (address.getId() == null && address.getId() != 0) {
            throw new BusinessException(ErrorMessageEnum.Address_ID_NOT_Empty_Object);
        }

        return validate(obj);
    }

    @Override
    public Boolean validateForDelete(Object obj) throws BusinessException {
        // TODO Auto-generated method stub
        return true;
    }

    private void getFullAddress(AddressDTO addressDTO) {
        StringBuffer fullNameCode = new StringBuffer();
        StringBuffer fullName = new StringBuffer();
        fullNameCode.append(addressDTO.getProvince());
        if (fullNameCode.length() > 0) {
            List<Code> codeList = codeService.getCodesByIds(fullNameCode.toString());
            String province = null;
            String city = null;
            for (Code code : codeList) {
                if (code.getCode().equals(addressDTO.getProvince())) {
                    province = code.getCodeDescZh();
                }
                if (code.getCode().equals(addressDTO.getCity())) {
                    city = code.getCodeDescZh();
                }
            }
            fullName.append(province == null ? "" : province).append(city == null ? "" : city).append(addressDTO.getAddressLine1());
            addressDTO.setFullName(fullName.toString());
        }
    }

    @Override
    public AddressDto queryDetailById(Integer id) {
        return addressReadMapper.queryDetailById(id);
    }
}
